package com.bluesky.jwf.component;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import com.bluesky.javawebbrowser.domain.html.tags.TagType;
import com.bluesky.javawebbrowser.domain.html.tags.form.TextArea;
import com.bluesky.javawebbrowser.domain.html.tags.form.input.InputType;
import com.bluesky.javawebbrowser.domain.html.tags.form.input.Text;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

/**
 * only one line of text
 * 
 * @author jack
 * 
 */
public class TextField extends Component implements StringField {
	public static enum Mode {
		Text, Password
	};

	private Text text;

	private Mode mode = Mode.Text;

	public String getText() {
		String s;

		s = text.getValue();

		if (s == null)
			s = "";
		return s;
	}

	public void setText(String text) {

		this.text.setValue(text);

	}

	@Override
	public void init(String html) {

		super.init(html);

		text = (Text) loadByJwfClass("textbox");

	}

	@Override
	public void setId(String id) {
		super.setId(id);
		text.setName(getId() + "_textbox");

	}

	@Override
	public void setPostBackData(String name, String value) {
		super.setPostBackData(name, value);

		if (name.equals("textbox"))
			setText(value);
	}

	@Override
	public String getString() {

		return getText();
	}

	public Integer getSize() {
		try {
			return Integer.valueOf(text.getSize());
		} catch (Exception e) {
			return null;
		}
	}

	public void setSize(Integer size) {
		String s = String.valueOf(size);

		if (size == null)
			s = null;

		text.setSize(s);

	}

	public Mode getMode() {
		return mode;
	}

	public void setMode(Mode mode) {
		this.mode = mode;
		if (mode == Mode.Text)
			text.setInputType(InputType.TEXT);
		else
			text.setInputType(InputType.PASSWORD);
	}

}
